﻿using System;
using System.Collections.Generic;
using System.Web;

namespace CodeCounsel.SharePoint2010.DigiD
{
    static class Redirector
    {
        public static void Redirect(HttpContext context, Uri targetUri, Action<Dictionary<string, string>> parameterBuilder)
        {
            context.Response.Redirect(GetFormattedUri(targetUri, parameterBuilder).ToString(), false);
        }

        static Uri GetFormattedUri(Uri targetUri, Action<Dictionary<string, string>> parameterBuilder)
        {
            UriBuilder clientRedirectBuilder = new UriBuilder(targetUri);
            Dictionary<string, string> clientRedirectQuery = clientRedirectBuilder.GetQuery();
            parameterBuilder(clientRedirectQuery);
            clientRedirectBuilder.SetQuery(clientRedirectQuery);
            return clientRedirectBuilder.Uri;
        }
    }
}
